﻿using System.Collections.Generic;
using System.Text;

namespace tiny_compiler
{
	public class Copy :
		IOperator
	{
		public Copy(string _targetValueName, string _targetOffsetValueName, string _sourceValueName)
		{
			m_targetValueName = _targetValueName;
			m_targetOffsetValueName = _targetOffsetValueName;
			m_sourceValueName = _sourceValueName;
		}

		#region IOperator Members

		public int Length
		{
			get { return Compiler.NameLength + Compiler.LengthLength + (Compiler.NumberLength * 3) + Compiler.LengthLength; }
		}

		public string Name
		{
			get { return string.Empty; }
		}

		public void Resolve(IEnumerable<IOperator> _operators)
		{
			m_targetValue = _operators.Offset(m_targetValueName);
			m_targetOffsetValue = _operators.Offset(m_targetOffsetValueName);
			m_sourceValue = _operators.Offset(m_sourceValueName);
			m_argLength = _operators.Operator(m_sourceValueName).Length;
		}

		public void Append(StringBuilder _data)
		{
			_data.Append(Compiler.CopyName);
			_data.Append(CompilerUtility.ConvertLength(Length));
			_data.Append(CompilerUtility.ConvertNumber(m_targetValue));
			_data.Append(CompilerUtility.ConvertNumber(m_targetOffsetValue));
			_data.Append(CompilerUtility.ConvertNumber(m_sourceValue));
			_data.Append(CompilerUtility.ConvertLength(m_argLength));
		}

		#endregion

		private string m_targetValueName;
		private int m_targetValue;
		private string m_targetOffsetValueName;
		private int m_targetOffsetValue;
		private string m_sourceValueName;
		private int m_sourceValue;
		private int m_argLength;
	}
}
